class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n = arr.size();
        unordered_map<int,int> hash;
        for(int i = 0;i < n;i++){
            hash[arr[i]] = i;
        }
    
        int Max = 0;
        vector<vector<int>> dp(n,vector<int>(n,2));
        
        for(int i = 2;i < n;i++){
            for(int j = i-1;j > 0;j--){
                int k = arr[i] - arr[j];
                if(hash.count(k)){
                    int ind = hash[k];
                    if(ind < j){
                        dp[j][i] = max(dp[j][i],dp[ind][j]+1);
                        Max = max(dp[j][i],Max);
                    }
                }
            }
        }
        return Max;
    }
};

